
$(function () {
	function getRadioValue(radioName) {
		var chkRadio = document.getElementsByName(radioName);
		for (var i = 0; i < chkRadio.length; i++) {
			if (chkRadio[i].checked)
				return chkRadio[i].value;
		}
	}
	$("#op_reset").on("click", function () {
		$("#attentionOp").val("");
	})

	$("#time_reset").on("click", function () {
		$("#attentionDate").val("");
	})
	$("table").delegate(".atdelete", "click", function () {
    var input = $(this).parent().parent().find("input");
    var tr = $(this).parent().parent();
    var atId = input.val();
    var r = confirm("确定要删除该通知吗？")
    if (r == true) {
        $.ajax({
            type: 'post',
            url: 'messageDelete/'+atId,
            success: function (reg) {
                tr.remove();
                alert(reg);
            },
            error: function () {
                alert("服务器发生错误，动态通知失败！")
            }
        });
    }
	})
	$("#op_search").on("click", function () {
		if($("#attentionOp").val().trim()!=""){
			var op = $("#attentionOp").val();
			$.ajax({
				type: "get",
				url: 'getAttentionByName/',
				async: true,
				cache: false,
				data: {da:op},
				success: function (reg) {
					if (reg) {
						$("#attentionTable").text("");
						$('#atPage').show();
						$('#atPage').pagination({
							totalData:reg.length,
							showData:5,
							coping:true,
							callback:function(api){
								$("#attentionTable").text("");
								var str = "";
								for (var i = api.getCurrent()*5-5; i <api.getCurrent()*5&&i < reg.length; i++) {

									str += "<tr class=''><td class='col-lg-1'>";
									str += "<input type='checkbox' name='atselect' value="+reg[i].msg_id+">";
									str += "</td><td class='col-lg-6'>";
									str += reg[i].content;
									str += "</td><td class='col-lg-2'>";
									str += reg[i].user_name;
									str += "</td><td class='col-lg-2'>";
									str += reg[i].time;
									str += "</td><td class='col-lg-1'>";
									str += "<a class='atdelete'>删除</a>"
									str += "</td></tr>";

								}
								$("#attentionTable").append(str);


							}
						},function(api){
							$("#attentionTable").text("");
							var str = "";
							for (var i = 0; i <5&&i < reg.length; i++) {
						//$(".myTr").remove();
						str += "<tr class=''><td class='col-lg-1'>";
						str += "<input type='checkbox' name='atselect' value="+reg[i].msg_id+">";
						str += "</td><td class='col-lg-6'>";
						str += reg[i].content;
						str += "</td><td class='col-lg-2'>";
						str += reg[i].user_name;
						str += "</td><td class='col-lg-2'>";
						str += reg[i].time;
						str += "</td><td class='col-lg-1'>";
						str += "<a class='atdelete'>删除</a>"
						str += "</td></tr>";
						
					}
					$("#attentionTable").append(str);
				});
						
					}
					else{
						alert("没有相关通知");
					}
				}
			})
		}else{
			alert("未填写对象名");
		}
	});
	$("#time_search").on("click", function () {
		if($("#attentionDate").val().trim()!=""){
			var date = $("#attentionDate").val();
			$.ajax({
				type: "get",
				url: 'getAttentionByDate/'+date,
				async: true,
				cache: false,
				success: function (reg) {
					if (reg) {
						$("#attentionTable").text("");
						$('#atPage').show();
						$('#atPage').pagination({
							totalData:reg.length,
							showData:5,
							coping:true,
							callback:function(api){
								$("#attentionTable").text("");
								var str = "";
								for (var i = api.getCurrent()*5-5; i <api.getCurrent()*5&&i < reg.length; i++) {

									str += "<tr class=''><td class='col-lg-1'>";
									str += "<input type='checkbox' name='atselect' value="+reg[i].msg_id+">";
									str += "</td><td class='col-lg-6'>";
									str += reg[i].content;
									str += "</td><td class='col-lg-2'>";
									str += reg[i].user_name;
									str += "</td><td class='col-lg-2'>";
									str += reg[i].time;
									str += "</td><td class='col-lg-1'>";
									str += "<a class='atdelete'>删除</a>"
									str += "</td></tr>";

								}
								$("#attentionTable").append(str);


							}
						},function(api){
							$("#attentionTable").text("");
							var str = "";
							for (var i = 0; i <5&&i < reg.length; i++) {
						//$(".myTr").remove();
						str += "<tr class=''><td class='col-lg-1'>";
						str += "<input type='checkbox' name='atselect' value="+reg[i].msg_id+">";
						str += "</td><td class='col-lg-6'>";
						str += reg[i].content;
						str += "</td><td class='col-lg-2'>";
						str += reg[i].user_name;
						str += "</td><td class='col-lg-2'>";
						str += reg[i].time;
						str += "</td><td class='col-lg-1'>";
						str += "<a class='atdelete'>删除</a>"
						str += "</td></tr>";
						
					}
					$("#attentionTable").append(str);
				});
					}
					else{
						alert("没有相关通知");
					}
				}
			})
		}else{
			alert("未填写时间");
		}
	});
	$("#Atselect_all").on("click", function () { 
		$("[name='atselect']").attr("checked",'true');//全选 
	}) 
	$("#Atdelete_all").on("click", function () { 
		var input = $("#attentionTable").find("input");
		var j = [];
		var k = 0;
		var r = confirm("确定要删除这些通知吗？")
		if (r == true) {
			for (var i = 0; i < input.length; i++) {
				var row = {};
				if (input.eq(i).is(':checked')) {
					row.atid = input.eq(i).val();
					j.push(row);
					k++;
				}
			}
			if(k==0){
				alert("未选中项目");
				return false;
			}
			$.ajax({
				type: "post",
				url: "deleteSelectAttention",
				data: { da: j},
            // headers: {
            //     Accept: "application/json",
            //     "Content-Type": "application/json"
            // },
            success: function (reg) {
            	for (var i = 0; i < input.length; i++) {
            		if (input.eq(i).is(':checked')) {
            			input.eq(i).parent().parent().remove();
            		}
            	}

            },
            error: function () {
            	alert("服务器发生错误，删除失败！");
            }
        })
		}
	}) 


})
